/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import { Module } from '@ohos/hvigor';
import { PackageJson } from 'type-fest';

import { ModuleJson } from '../../../options/configure/module-json-options.js';

import { DefaultModuleDependency } from './default-module-dependency.js';
import { DefaultNpmDependency } from './default-npm-dependency.js';
import { DependencyEnum, DependencyType } from './dependency-interface.js';
import ModuleOptObj = ModuleJson.ModuleOptObj;

export class DependencyBuilder {
  private _module: Module | undefined;
  private _dependencyName: string | undefined;
  private _dependedVersion: string | undefined;
  private _lastDependencyName: string | undefined;
  private _dependencyVersion: string | undefined;
  private _dependencyRootPath: string | undefined;
  private _dependencies: Partial<Record<string, string>> | undefined;
  private _pkgJsonPath: string | undefined;
  private _pkgJsonObj: PackageJson | undefined;
  private _dependencyType: DependencyType | undefined;
  private _dependencyEnum: DependencyEnum | undefined;
  private _isLocal = false;
  private _moduleJsonObj: ModuleOptObj | undefined;
  private _isModuleDependency = true;

  setIsModuleDependency(value: boolean) {
    this._isModuleDependency = value;
    return this;
  }

  isModuleDependency(): boolean {
    return this._isModuleDependency;
  }

  buildDefaultNpmDependency() {
    return new DefaultNpmDependency(this);
  }

  buildDefaultModuleDependency() {
    return new DefaultModuleDependency(this);
  }

  setModule(module: Module) {
    this._module = module;
    return this;
  }

  setDependencyName(dependencyName: string) {
    this._dependencyName = dependencyName;
    return this;
  }

  setDependedVersion(dependedVersion: string) {
    this._dependedVersion = dependedVersion;
    return this;
  }

  setDependencyRootPath(dependencyRootPath: string) {
    this._dependencyRootPath = dependencyRootPath;
    return this;
  }

  setPkgJsonPath(pkgJsonPath: string) {
    this._pkgJsonPath = pkgJsonPath;
    return this;
  }

  setPkgJsonObj(pkgJsonObj: PackageJson) {
    this._pkgJsonObj = pkgJsonObj;
    this._dependencyVersion = this._pkgJsonObj.version;
    this._dependencies = this._pkgJsonObj.dependencies;
    return this;
  }

  setDependencyType(dependencyType: DependencyType) {
    this._dependencyType = dependencyType;
    return this;
  }

  setDependencyEnum(dependencyEnum: DependencyEnum) {
    this._dependencyEnum = dependencyEnum;
    return this;
  }

  setIsLocal(isLocal: boolean) {
    this._isLocal = isLocal;
    return this;
  }

  setModuleJsonObj(moduleJsonObj?: ModuleOptObj) {
    this._moduleJsonObj = moduleJsonObj;
    return this;
  }

  setLastDependencyName(lastDependencyName: string) {
    this._lastDependencyName = lastDependencyName;
    return this;
  }

  getModule(): Module {
    return this._module!;
  }

  getDependencyName(): string {
    return this._dependencyName!;
  }

  getDependedVersion(): string {
    return this._dependedVersion!;
  }

  getDependencyVersion(): string {
    return this._dependencyVersion!;
  }

  getDependencyRootPath(): string {
    return this._dependencyRootPath!;
  }

  getPkgJsonPath(): string {
    return this._pkgJsonPath!;
  }

  getPkgJsonObj(): PackageJson {
    return this._pkgJsonObj!;
  }

  getDependencies(): Partial<Record<string, string>> {
    return this._dependencies!;
  }

  getDependencyType(): DependencyType {
    return this._dependencyType!;
  }

  getDependencyEnum(): DependencyEnum {
    return this._dependencyEnum!;
  }

  getLastDependencyName(): string | undefined {
    return this._lastDependencyName;
  }

  isLocal(): boolean {
    return this._isLocal;
  }

  getModuleJsonObj(): ModuleOptObj | undefined {
    return this._moduleJsonObj;
  }
}
